package com.biz.primus.ms.auth.api;

import com.biz.primus.base.enums.CommonStatus;
import com.biz.primus.model.sso.auth.vo.RoleVo;
import com.biz.primus.ms.auth.dao.po.Role;
import com.biz.primus.ms.auth.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by younger on 17/11/6.
 */
@RestController
@RequestMapping("soa/roleService")
public class RoleApiController {

    private final RoleService roleService;

    @Autowired
    public RoleApiController(RoleService roleService){
        this.roleService = roleService;
    }

    @ApiOperation("获取用户的权限")
    @PostMapping("findByStatus")
    public List<Role> findByStatus(@RequestBody CommonStatus status){
        List<Role> roleList = roleService.findByStatus(status);
        return roleList;
    }

    @ApiOperation("修改更新角色")
    @PostMapping("addOrUpdate")
    public void addOrUpdate(@RequestBody RoleVo roleVo){
        Role role = new Role();
        BeanUtils.copyProperties(roleVo, role);
        roleService.addOrUpdate(role);
    }

    @ApiOperation("找查一个角色")
    @PostMapping("getRole")
    public RoleVo getRole(@RequestParam(name = "id") Long id){
        RoleVo resRole = new RoleVo();
        Role role = roleService.getRole(id);
        BeanUtils.copyProperties(role, resRole);
        return resRole;
    }

    @ApiOperation("删除一个角色")
    @PostMapping("delete")
    public Boolean delete(@RequestParam(name = "id") Long id){
        //逻辑删除,不是物理删除。
        if (id != null) {
            roleService.delete(id);
            return true;
        }
        return false;
    }

    @ApiOperation("判断角色是否存在")
    @PostMapping("isExist")
    public Boolean isExist(@RequestBody RoleVo roleVo) {
        Role role = new Role();
        BeanUtils.copyProperties(roleVo, role);
        return roleService.isExist(role);
    }
}
